#include "cctpthrdsavetick.h"
#include"ctpdatastorage.h"
#include"../CommUtil/ComFun.h"

CCTPThrdSaveTick::CCTPThrdSaveTick()
{
    m_iPre5Minute = -1;
    m_iPre1Minute = -1;
    m_strPreDay  = "";
}

CCTPThrdSaveTick::~CCTPThrdSaveTick()
{
    qDebug()<<" Thread Name:"<<m_szThrdName <<" is over.";
}

void CCTPThrdSaveTick::run(){
    OracleDAO dao(m_szThrdName);
    if(m_szThrdName == QString("CTPSaveDayKlineThrd")){
        //保存日K线的线程
        while(true){
            QTime t = QTime::currentTime();
            if(t.hour() == 16 && t.minute() == 30  ){ // 16:30 o'clock
                QDate oDate = QDate::currentDate();
                if(6 != oDate.dayOfWeek() && 7 != oDate.dayOfWeek() &&
                   m_strPreDay != QString::number(oDate.day())){//当前日期如果是周末 则不进行日K的处理

                    QString strPreTradeDay = dao.GetPreTradeDay(m_szThrdName);//获取前一交易日的日期
                    dao.GetAllPreVolume(m_szThrdName,strPreTradeDay);//获取全部合约的前一日的成交量
                    SaveDayKlineData(dao);//保存当日所有合约的日K线数据
                    m_strPreDay  = QString::number(oDate.day());
                    m_iPre5Minute = -1;
                    m_iPre1Minute = -1;//存储当天日K之后 重置m_iPreMinute为-1
                }
            }else{
                //Wait 50s
                CComFun::SleepSecs(50);
            }
        }
    }else if(m_szThrdName == QString("CTPSave5MinKlineThrd")){//保存5分钟K线的线程
        while(true) {
            Save5MinKLineData(dao);
        }
    }else if(m_szThrdName == QString("CTPSave1MinKlineThrd")){//保存1分钟K线的线程
        ///暂不处理1分钟的K线数据
        //while(true){
            //Save1MinKLineData(dao);
        //}
    }else{
        //保存行情数据
		QStringList szHqList;
        while(true) {
            CTPDataStorage::GetData(szHqList, m_nIndex);
            if(szHqList.size() > 0) {
                //保存行情数据
				dao.UpdateCTPMarketInfo(szHqList, m_szThrdName);
                //计算5分钟K线数据
                Calc5MinKLineData(szHqList);
                //计算1分钟K线数据
                //Calc1MinKLineData(szHqList);
            }
        }
    }
}
//保存5分钟K线数据
void CCTPThrdSaveTick::Save5MinKLineData(OracleDAO &dao){
    QTime t = QTime::currentTime();
    if(t.minute()%5 == 0 && t.second() == 0 && m_iPre5Minute != t.minute() ){
        if(m_iPre5Minute == -1){
            m_MapContract.clear();
            m_MapZhulian.clear();
            m_vMainContracts.clear();
            m_MapJinyue.clear();

            if(true != dao.GetInstruments(m_szThrdName,m_vMainContracts,m_MapContract) || m_MapContract.size() == 0 ){
                qDebug("Get all Instruments Failure");
            }
            if(true != dao.GetZhulian(m_szThrdName,m_MapZhulian)){
                qDebug()<<" Get Zhulian Failure.";
            }
            if(true != dao.GetJinyue(m_szThrdName,m_MapJinyue)){
                qDebug()<<" Get Jinyue Failure.";
            }

            QString strPreTradeDay = dao.GetPreTradeDay(m_szThrdName);//获取前一交易日的日期
            dao.GetAllPre5MinKLine(m_szThrdName,strPreTradeDay);//获取所有合约的前一交易日最后一根5分钟K线的信息
        }

        switch(t.hour()){
        case 9:///日盘
        case 21:///夜盘
            if(t.minute() > 0){// 9:00 和 21:00的时候不保存5分钟K线
                CTPDataStorage::Save5MinKLineData(dao,m_MapZhulian,m_MapJinyue,m_MapContract);//保存5分钟K线
            }
            break;
        case 22:
        case 23:
        case 0:
        case 1:
        case 10:
        case 13:
        case 14:
            CTPDataStorage::Save5MinKLineData(dao,m_MapZhulian,m_MapJinyue,m_MapContract);//保存5分钟K线
            break;
        case 2:
        case 11:
            if(t.minute() <= 30){
                CTPDataStorage::Save5MinKLineData(dao,m_MapZhulian,m_MapJinyue,m_MapContract);//保存5分钟K线
            }
            break;
        case 15:
            if(t.minute() <= 15){
                CTPDataStorage::Save5MinKLineData(dao,m_MapZhulian,m_MapJinyue,m_MapContract);//保存5分钟K线
            }
            break;
        default:
            break;
        }
        m_iPre5Minute = t.minute();
    }
    else{
        CComFun::SleepSecs(0.1);
    }
}
//保存1分钟K线数据
void CCTPThrdSaveTick::Save1MinKLineData(OracleDAO &dao){
    QTime t = QTime::currentTime();

    if(t.second() == 0 && m_iPre1Minute != t.minute() ){
        if(m_iPre1Minute == -1){
            m_MapContract.clear();
            m_MapZhulian.clear();
            m_vMainContracts.clear();
            m_MapJinyue.clear();

            if(true != dao.GetInstruments(m_szThrdName,m_vMainContracts,m_MapContract) || m_MapContract.size() == 0 ){
                qDebug("Get all Instruments Failure");
            }
            if(true != dao.GetZhulian(m_szThrdName,m_MapZhulian)){
                qDebug()<<" Get Zhulian Failure.";
            }
            if(true != dao.GetJinyue(m_szThrdName,m_MapJinyue)){
                qDebug()<<" Get Jinyue Failure.";
            }
        }

        switch(t.hour()){
        case 9:///日盘
        case 21:///夜盘
            if(t.minute() > 0){// 9:00 和 21:00的时候不保存5分钟K线
                CTPDataStorage::Save1MinKLineData(dao,m_MapZhulian,m_MapJinyue,m_MapContract);//保存1分钟K线
            }
            break;
        case 22:
        case 23:
        case 0:
        case 1:
        case 10:
        case 13:
        case 14:
            CTPDataStorage::Save1MinKLineData(dao,m_MapZhulian,m_MapJinyue,m_MapContract);//保存1分钟K线
            break;
        case 2:
        case 11:
            if(t.minute() <= 30){
                CTPDataStorage::Save1MinKLineData(dao,m_MapZhulian,m_MapJinyue,m_MapContract);//保存1分钟K线
            }
            break;
        case 15:
            if(t.minute() <= 15){
                CTPDataStorage::Save1MinKLineData(dao,m_MapZhulian,m_MapJinyue,m_MapContract);//保存1分钟K线
            }
            break;
        default:
            break;
        }
        m_iPre1Minute = t.minute();
    }
    else{
        CComFun::SleepSecs(0.1);
    }
}
//计算5分钟K线数据
void CCTPThrdSaveTick::Calc5MinKLineData(QStringList &szHqList){
    double  lastPrice       = szHqList.at(4).toDouble();
    if(lastPrice != 1.79769e+308 ){
        //更新合约对应的5分钟K线数据
        CTPDataStorage::Update5MinKLineData(szHqList,m_nIndex);
    }
}
//计算1分钟K线数据
void CCTPThrdSaveTick::Calc1MinKLineData(QStringList &szHqList){
    QString strInstrumentID = szHqList.at(1);
    double  lastPrice       = szHqList.at(4).toDouble();
    long    volume          = szHqList.at(11).toLong();
    if(lastPrice != 1.79769e+308 ){
        //更新合约对应的1分钟K线数据
        CTPDataStorage::Update1MinKLineData(strInstrumentID,lastPrice,volume,m_nIndex);
    }
}

void CCTPThrdSaveTick::SaveDayKlineData(OracleDAO &dao){
    QDate qTemp = QDate::currentDate();
    QString date = qTemp.toString("yyyyMMdd");
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    qDebug("**************************************************");
    qDebug("**********START SAVE THE DAY KLINE DATA***********");
    qDebug("**************************************************");
    QMap<QString,ContractType> MapContractType;
    std::vector<QString> vMainContracts;
    std::vector<QString> vContractType;
    if(false == dao.GetInstruments(m_szThrdName,vMainContracts,MapContractType) || vMainContracts.size() == 0 ){
        qDebug("Get all Instruments Failure");
    }
    ///保存所有合约的日K线数据
    for(int i = 0; i < vMainContracts.size() ; i++){
        dao.SaveDayKLineData(vMainContracts[i],date,m_szThrdName);
    }
    qDebug("**************************************************");
    qDebug("***********SAVE THE DAY KLINE DATA END************");
    qDebug("**************************************************");
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    qDebug("**************************************************");
    qDebug("***********MAKE THE Zhulian Record START**********");
    qDebug("**************************************************");
    if( false == dao.GetContractTypes(m_szThrdName,vContractType) || vContractType.size() == 0){
        qDebug("Get all ContractType Failure");
    }
    for(int i = 0; i < vContractType.size(); i++){
        QString MainContractCode = "";
        if(true == dao.GetTheMainContractCode(m_szThrdName,vContractType[i],date,MainContractCode)){
            QString zhulianCode = vContractType[i] + "Zhulian";
            ///更新主连表
            dao.InsertZhulianRec(m_szThrdName,zhulianCode,date,MainContractCode);
            ///制作主连日K线
            dao.SaveZhulianDayKLineData(m_szThrdName,zhulianCode,date,MainContractCode,MapContractType);
            ///制作主连Tick数据
            //dao.SaveZhulianTickData(m_szThrdName,zhulianCode,date,MainContractCode);
        }
    }
    qDebug("**************************************************");
    qDebug("***********MAKE THE Zhulian Record END************");
    qDebug("**************************************************");
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    qDebug("**************************************************");
    qDebug("***********MAKE THE Jinyue Record START***********");
    qDebug("**************************************************");
    for(int i = 0; i < vContractType.size(); i++){
        //获取各品种对应的近月合约代码
        QString jinyueContractCode = "";
        if(true == dao.GetTheJinyueContractCode(m_szThrdName,vContractType[i],jinyueContractCode)){
            QString jinyueCode = vContractType[i] + "Jinyue";
            //更新近月表
            dao.InsertJinyueRec(m_szThrdName,jinyueCode,date,jinyueContractCode);
            //制作近月日K线
            dao.SaveJinyueDayKLineData(m_szThrdName,jinyueCode,date,jinyueContractCode);
            //制作近月Tick数据
            //dao.SaveJinyueTickData(m_szThrdName,jinyueCode,date,jinyueContractCode);
        }
    }
    qDebug("**************************************************");
    qDebug("***********MAKE THE Jinyue Record END*************");
    qDebug("**************************************************");
}
